﻿package com.dykj.platform.iadapter.tags;

import java.io.IOException;
import java.io.Writer;

import org.apache.struts2.components.Component;
import java.util.logging.Logger;
import com.opensymphony.xwork2.util.ValueStack;
import java.util.logging.Level;
import com.dykj.platform.iadapter.utils.StringUtils;
public class Input extends Component {
	private String name;
	private String value;
	private String editable;
	private String type;
	private String attr;
	private String model;

	@Override
	public boolean start(Writer arg0) {
		boolean result = super.start(arg0);
		StringBuffer str = new StringBuffer();
		try {
			if(value!=null){
				if(value.startsWith("%{")&&value.endsWith("}")){
					value=value.substring(2, value.length()-1);
					value = StringUtils.objToStr(this.getStack().findValue(value));
				}else if(value.startsWith("#")){
					value = StringUtils.objToStr(this.getStack().findValue(value));
				}
			}else{
				value=StringUtils.objToStr(this.getStack().findValue(name));
			}
			if(value==null||value.equals("null"))
				value="";
			if(editable!=null){
				if(editable.startsWith("%{")&&editable.endsWith("}")){
					editable = editable.substring(2, editable.length()-1);
					editable = StringUtils.objToStr(this.getStack().findValue(editable));
				}
			}else{
				editable = StringUtils.objToStr(this.getStack().findValue("requestMsg['editable']"));
			}
			String in_id="";
			if(name.indexOf(".")>0&&model.equals("true")&&!type.equals("hidden")){
				in_id=name.substring(name.indexOf(".")+1);
			}else{
				in_id=name;
			}
			if(editable==null)
				editable="true";
			if (!type.equals("hidden")&&editable.equals("false")) {
				str.append("<label>");
				str.append(value);
				str.append("</label");
			} else {
				str.append("<input type='");
				str.append(type);
				str.append("' name='");
				str.append(in_id);
				str.append("' id='");
				str.append(in_id);
				str.append("' value='");
				str.append(value);
				str.append("' ");
				if(attr!=null){
					String array[]= attr.split(":");
					str.append(array[0]);
					str.append("='");
					str.append(array[1]);
					str.append("' ");
				}

				if(type.equals("radio") || type.equals("checkbox")){
					if(value.equals("")){
						str.append("disabled='true'");
					}else{
						String ss = StringUtils.objToStr(this.getStack().findValue(name));
						if("radio".equals(type)&&value.equals(ss)){
							str.append("checked");
						}
						if("checkbox".equals(type)&&ss.indexOf(value)>-1){
							str.append("checked");
						}
					}
	
				}
				str.append("/>");
			}
			arg0.write(str.toString());
		} catch (IOException e) {
			Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, e);
		}
		return result;
	}

	public String getEditable() {
		return editable;
	}

	public void setEditable(String editable) {
		this.editable = editable;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	public String getAttr() {
		return attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}

	public Input(ValueStack arg0) {
		super(arg0);
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}
}
